package org.jake.nb.ext.ant.hints.impl;

import java.util.Collection;
import java.util.HashSet;
import javax.swing.text.Document;
import org.jake.nb.ext.ant.hints.AntHint;
import org.jake.nb.ext.ant.hints.Configuration;
import org.jake.nb.ext.ant.hints.Paths;
import org.jake.nb.ext.ant.hints.SpanContainer;
import org.jake.nb.ext.ant.parser.Span;
import org.jake.nb.ext.ant.parser.TargetSpan;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author JakeJ
 */
@ServiceProvider(service=AntHint.class, path="org-jake-nb-ext-ant-hints")
public class TargetNameAttributeMissing implements AntHint {

  private Configuration configuration;

  public TargetNameAttributeMissing() {
    configuration = new Configuration(TargetNameAttributeMissing.class.getCanonicalName(),
            "Target's name attribute missing", "Indicates whether the name attribute of a target is missing.",
            true, Severity.ERROR, Paths.TARGET);
  }

  @Override
  public Collection<ErrorDescription> getErrors(final SpanContainer spanContainer) {
    final Collection<ErrorDescription> errors = new HashSet<ErrorDescription>();
    for (final Span span : spanContainer.getTargetSpans().getSpans()) {
      final TargetSpan targetSpan = (TargetSpan) span;
      if (targetSpan.getNameSpan() == null) {
        errors.add(createError(spanContainer.getDocument(), targetSpan));
      }
    }
    return errors;
  }

  private ErrorDescription createError(final Document document, final TargetSpan span) {

    final String text = NbBundle.getMessage(TargetNameAttributeMissing.class, "Desc_Err_TargetNameMissing");

    return ErrorDescriptionFactory.createErrorDescription(configuration.getSeverity(), text,
            document, NbEditorUtilities.getLine(document, span.getStart(), true).getLineNumber() + 1);
  }

  @Override
  public Configuration getConfiguration() {
    return configuration;
  }
}
